//SVGTransformer.cpp
//
#include <windows.h>
#include <limits>
#include <algorithm>
#include "stdafx.h"
#define EXPORTING_DLL
#include "SVGTransformer.h"
#include "SVGDrawing.h"
using namespace std;

//aqua black	blue	fuchsia gray	green	lime	maroon navy	 olive	purple	
//red silver	teal	yellow

const int strokeColorCount = 24;

_bstr_t svgcolor[strokeColorCount] = {_T("yellow"), _T("blue"), _T("green"), _T("red"), _T("aqua"), 
_T("fuchsia"), _T("gray"), _T("lime"), _T("maroon"), _T("navy"), _T("olive"), _T("purple"), _T("silver"),
_T("teal"), _T("brown"),_T("chocolate"), _T("blueviolet"), _T("hotpink"), _T("cadeblue"), _T("burlywood"),
_T("cornflowerblue"), _T("coral"), _T("darksalmon"), _T("black")};

BOOL APIENTRY DllMain( HANDLE hModule, 
                       DWORD  ul_reason_for_call, 
                       LPVOID lpReserved
					 )
{
    return TRUE;
}

LPTSTR readXMLData(void *handler, LPCTSTR value, CHandler *PHandler)
{
	
	LPTSTR temp;
	temp = (LPTSTR)malloc((250 + 1)*sizeof(TCHAR));
	PHandler->getValue(value, temp, 250, handler);
	return temp;
}

_bstr_t makeXPath(_bstr_t xpath1, int i, _bstr_t xpath2)
{
	if (i<0)
	{
		cerr<<"Error: "<<endl;
		exit(1);
	}
	if (i>0)
	{
		_bstr_t temp = _T("[");
		_bstr_t temp1 = (_variant_t)(int)i;
		temp+=temp1;
		temp+=_T("]");
		xpath1+=temp;
		xpath1+=xpath2;
	}
	return xpath1;
}



void draw(LPCTSTR fpath)
{
	double minrInner = 1.79769e+308;//numeric_limits<double>::max();
	double maxzTop = 0;
	double factor =300;
	int count=0;
	_bstr_t windingpath=_T("/_TdsDesignData_/_Data_/TechData/ActivePart/Winding");
	SVGDrawing test;
	_bstr_t path(_T(""));
	CHandler *PHandler;
	PHandler = new CHandler(path);
	
	// read data file with house information
	void * handler = PHandler->initHandlerFromFile(fpath, NULL);
	
	int windingCount = PHandler->getCount(windingpath,handler);
	for (int i=1; i<=windingCount; i++)
	{

		_bstr_t currentpath = makeXPath(windingpath, i, _T("/CurrentPath"));
		int currentpathCount = PHandler->getCount(currentpath, handler);
		
		for (int j=1; j<=currentpathCount; j++)
		{
			_bstr_t coordinatepath = makeXPath(currentpath, j, _T("/Coordinate"));
			int coordinateCount = PHandler->getCount(coordinatepath, handler);
			for (int k=1; k<=coordinateCount; k++)
			{
				_bstr_t rInner = makeXPath(coordinatepath, k, _T("/rInner"));
				_bstr_t rOuter = makeXPath(coordinatepath, k, _T("/rOuter"));
				_bstr_t zBottom = makeXPath(coordinatepath, k, _T("/zBottom"));
				_bstr_t zTop = makeXPath(coordinatepath, k, _T("/zTop"));
				
				double dInner = PHandler->getValueDouble(rInner, handler)*factor;
				double dOuter = PHandler->getValueDouble(rOuter, handler)*factor;
				double dBottom = PHandler->getValueDouble(zBottom, handler)*factor;
				double dTop = PHandler->getValueDouble(zTop, handler)*factor;
				if (i==1 && j==1 && k==1) minrInner = dInner;
				minrInner = min(minrInner, dInner);
				maxzTop = max(maxzTop, dTop);
			}
		}
	}
	int scc = 0;
	for (int i=1; i<=windingCount; i++)
	{	
		_bstr_t currentpath = makeXPath(windingpath, i, _T("/CurrentPath"));
		int currentpathCount = PHandler->getCount(currentpath, handler);
		
		for (int j=1; j<=currentpathCount; j++)
		{
			if(scc<strokeColorCount)
				scc++;
			_bstr_t coordinatepath = makeXPath(currentpath, j, _T("/Coordinate"));
			int coordinateCount = PHandler->getCount(coordinatepath, handler);
			for (int k=1; k<=coordinateCount; k++)
			{
				_bstr_t rInner = makeXPath(coordinatepath, k, _T("/rInner"));
				_bstr_t rOuter = makeXPath(coordinatepath, k, _T("/rOuter"));
				_bstr_t zBottom = makeXPath(coordinatepath, k, _T("/zBottom"));
				_bstr_t zTop = makeXPath(coordinatepath, k, _T("/zTop"));
				
				double dInner = PHandler->getValueDouble(rInner, handler)*factor;
				double dOuter = PHandler->getValueDouble(rOuter, handler)*factor;
				double dBottom = PHandler->getValueDouble(zBottom, handler)*factor;
				double dTop = PHandler->getValueDouble(zTop, handler)*factor;
				double ty = maxzTop-dTop;
				double tx = dInner-minrInner;
				
				if((windingCount*currentpathCount)<=strokeColorCount)
					test.setFillColor(svgcolor[scc]);
				else
					test.setFillColor(_T("black"));
					
				test.createRect(tx, ty, dOuter-dInner, dTop-dBottom);

				count++;
			}
		}
	}
	cout<<count<<endl;
	delete PHandler;
	test.saveToFile(".//TdsDesign.svg");
}
